import {
    createQuickBooking,
    deleteById,
    getById,
    getByIdDetail,
    getListForPage,
    getOpenListForPage,
    getUserQuickBooking,
    updateOnlineBooking,
    updateQuickBooking
} from '../../services/logisticsOrder/quickBooking';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'quickBooking',
    state: {
        page: {},
        list: [],
        dataSource: {}, //快速订舱详情表
        quickBooking:{}, //快速订舱表
        flag: []    //存在数据则不可新增交接单
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询订舱
         * 
         */ 
        *getListForPage({ payload }, { call, put }) {
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据id获取订舱单
         *
         * @param id
         */
        *getById({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },

        /**
         * 新增订舱单
         *
         * @param call
         */
        *createQuickBooking({ payload }, { call }) {
            const res = yield call(createQuickBooking, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("订舱单新增成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 修改订舱单(快速订舱)
         *
         * @param call
         * @param put
         * @param select
         */
        *updateQuickBooking({ payload }, { call, put }) {
            const res = yield call(updateQuickBooking, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok"  ) {
                if(payload.status=="4"){
                    message.info("已同意作废！");
                }else if(payload.status == "5"){
                    message.info("已成功提交仲裁！");
                }else {message.info("订舱单更新成功！");}
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除订舱单
         *
         * @param id
         */
        *deleteById({payload}, {call}) {
            const res = yield call(deleteById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("订舱单删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据status获取当前用户的订舱信息
         *
         * @param status
         * @return
         */
        *getUserQuickBooking({ payload }, { call, put }){
            const res = yield call(getUserQuickBooking, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        flag: res.data.content
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },


        /**
         * 分页查询订舱详情
         *
         */
            *getOpenListForPage({ payload }, { call, put }) {
            const res = yield call(getOpenListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        // dataSource: res.data.content,
                        list: res.data.content.list ? res.data.content.list : [],
                        // page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                        page: res.data.content.page ? res.data.content.page :[],
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据id获取订舱单详情(官网订舱)
         *
         * @param id
         */
            *getByIdDetail({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getByIdDetail, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content.detail,
                            quickBooking: res.data.content.quickBooking
                        }
                    });
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },

        /**
         * 修改订舱单(在线订舱)
         *
         * @param call
         * @param put
         * @param select
         */
            *updateOnlineBooking({ payload }, { call, put }) {
            const res = yield call(updateOnlineBooking, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok"  ) {
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};